﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Drawing;

namespace TestGraphics
{
    public class Cube : Shape
    {
        private static int[] cubeIndices = new int[] 
            {
                // Top
                2, 3, 7, 6, 
                // Bottom
                0, 1, 5, 4, 
                // Front
                0, 1, 3, 2, 
                // Right
                1, 5, 7, 3, 
                // Back
                4, 5, 7, 6,
                // Left
                0, 2, 6, 4
            };
        private static int[] cubeTextures = new int[] 
            {
                // Top
                2, 0, 1, 3, 
                // Bottom
                1, 3, 2, 0, 
                // Front
                1, 3, 2, 0, 
                // Right
                1, 3, 2, 0, 
                // Back
                1, 3, 2, 0, 
                // Left
                3, 2, 0, 1, 
            };
        private static Vector2[] textureCoords = new Vector2[]
            {
                new Vector2(0, 0),
                new Vector2(0, 1),
                new Vector2(1, 0),
                new Vector2(1, 1)
            };
        private static Vector3[] cubeCoords = new Vector3[]
            {
                new Vector3(0.5f, -0.5f, -0.5f),
                new Vector3(0.5f, -0.5f, 0.5f),
                new Vector3(0.5f, 0.5f, -0.5f),
                new Vector3(0.5f, 0.5f, 0.5f),
                new Vector3(-0.5f, -0.5f, -0.5f),
                new Vector3(-0.5f, -0.5f, 0.5f),
                new Vector3(-0.5f, 0.5f, -0.5f),
                new Vector3(-0.5f, 0.5f, 0.5f)
            };

        private Vector3 _middle;

        public Cube(Vector3 middle)
        {
            _middle = middle;
        }

        public override void Draw(string texture)
        {
            GL.Enable(EnableCap.Texture2D);
            GL.Color3(Color.White);

            TextureCache.Instance.SetCurrentTexture(texture);

            GL.Begin(BeginMode.Quads);

            for (int i = 0; i < cubeIndices.Length; i++)
            {
                GL.TexCoord2(textureCoords[cubeTextures[i]]);
                GL.Vertex3(_middle + cubeCoords[cubeIndices[i]]);
            }

            GL.End();
            GL.Disable(EnableCap.Texture2D);
        }
    }
}
